app.controller("readCatalogListCtrl",function($scope,$state,$ionicLoading,$http,$stateParams,$ionicSlideBoxDelegate){
    var user_info = JSON.parse(localStorage.getItem("UserInfo"));
    var bookInfo = JSON.parse(localStorage.getItem("bookInfo"));
    // var getPath = JSON.parse(localStorage.getItem("getPath"));
    var chapter;
    angular.element(document).ready(function(){

        $scope.bookName = user_info.bookName;
        $scope.getCard($stateParams.queryDate);
        // $scope.getCard(new Date().Format("yyyy-MM-dd"));
    });

    //获取书籍卡片
    $scope.getCard = function (queryDate) {
        //获取学习计划
        var url = getPath.url+getPath.name+"/app/getCycleCard";
        var param = {
            userId:user_info.userId,
            bookId:bookInfo.bookId,
        }
        if(!queryDate){
            param.queryDate = new Date().Format("yyyy-MM-dd");
        }else{
            param.queryDate = queryDate;
        }
        $http({
            url: url,
            method: "POST",
            headers:{'Content-Type':'application/x-www-form-urlencoded'},
            data: param
        }).success(function (data) {
                if (data.resultCode == 200) {
                    var res = data.resultBean;
                    //书名
                    $scope.bookName = res.bookInfoDTO.bookName;
                    //章节名称数组
                    var cycleDetaiList = res.cycleDetaiList;
                    var _len = cycleDetaiList.length;
                    var chapterNames = [];
                    var tempStr = "";
                    for(var i=0;i<_len;i++){
                        var temp = cycleDetaiList[i];
                        if(tempStr.indexOf(temp.chapterName)==-1){
                            tempStr = temp.chapterName;
                            chapterNames.push(temp);
                        }
                    }
                    $scope.chapterNames = chapterNames;
                    //目录
                    var cycleDetaiList = res.cycleDetaiList;
                    //章节下的卡片目录
                    $scope.cardList = cycleDetaiList;
                    //取今天第一章卡片
                    chapter = res.bookCardList[0];
                } else {
                    $ionicLoading.show({
                        template: "获取阅读章节失败",
                        duration: 3000
                    });
                }
            })
            .error(function () {
                $ionicLoading.hide();
                $ionicLoading.show({
                    template: "获取阅读章节失败",
                    duration: 3000
                });
            });
    }
    //下一章
    $scope.openNext = function () {
        //修改获取卡片的顺序
        $state.go("home.read-book",
            {
                cardNumber:chapter.cardNumber,
                taskDate:chapter.taskDate
            }
        );
    };
    //返回列表
    $scope.goBackCatalog = function () {
        $state.go("home.read-catalog");
    }
});